package com.chinasofti.demo;

import com.chinasofti.demo02.MyConnection2;
import com.chinasofti.demo03.MyConnection3;
import com.chinasofti.util.JDBCUtils;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

public class MyDataSource implements DataSource {
    //定义一个集合容器，用于保存多个数据库连接对象
    private static List<Connection> pool =
            Collections.synchronizedList(new ArrayList<Connection>());
    //静态代码块，生成10个数据库连接保存到集合
    static{
        for(int i=0;i<10;i++){
            Connection con = JDBCUtils.getConnection();
            pool.add(con);
        }
    }
    //返回连接池的大小
    public int getSize(){
        return pool.size();
    }
    //返回池中的一个数据库连接
    @Override
    public Connection getConnection() throws SQLException {
        if(pool.size() >0){
            //从池中获取数据库连接
            Connection con = pool.remove(0);
            //通过自定义连接对象进行包裹
            //MyConnection2 mycon = new MyConnection2(con,pool);
            MyConnection3 mycon = new MyConnection3(con,pool);
            return mycon;
        }else{
            throw new RuntimeException("连接数量已经用尽");
        }
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }
}